/*
 * Java_Espanol.java
 *
 * Created on 4 de enero de 2010, 06:40 PM
 */
package gui;

import clases.*;
import javax.swing.ImageIcon;

/**
 *
 * @author  christmo
 */
public class Java_Espanol extends javax.swing.JFrame {

    /** Creates new form Java_Espanol */
    public Java_Espanol() {
        initComponents();
        jbVerificar.setIcon(new ImageIcon("iconos/Run.png"));
        jbLimpiar.setIcon(new ImageIcon("iconos/limpiar.png"));
        jbSalir.setIcon(new ImageIcon("iconos/salir.png"));

        //Carga de Archivos
        Lexico.cargarArchivos();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jbVerificar = new javax.swing.JButton();
        jbSalir = new javax.swing.JButton();
        jbLimpiar = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jlIndicador2 = new javax.swing.JLabel();
        jlIndicador = new javax.swing.JLabel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtTxt = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtLexico = new javax.swing.JTextArea();
        jScrollPane3 = new javax.swing.JScrollPane();
        jtSintactico = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Javañol");

        jbVerificar.setText("Verificar Código");
        jbVerificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbVerificarActionPerformed(evt);
            }
        });

        jbSalir.setText("Salir");
        jbSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbSalirActionPerformed(evt);
            }
        });

        jbLimpiar.setText("Limpiar");
        jbLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbLimpiarActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel2.setText("pequeJavañol");

        jtTxt.setColumns(20);
        jtTxt.setFont(new java.awt.Font("Courier New", 0, 14)); // NOI18N
        jtTxt.setRows(5);
        jtTxt.setTabSize(5);
        jtTxt.setText("VARIABLES \nVAR _ID ENTERO;\nVAR _ID CADENA;\nINICIO \nREPETIR ( _ID == _NUM ){ \nESCRIBIR ( _NUM ) ; \n} \nSI ( TRUE ) { \n} SINO { \n_ID = _NUM + _NUM ; \n} \nFIN ");
        jtTxt.setToolTipText("Ingrese el Código...");
        jtTxt.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
        jtTxt.setMargin(new java.awt.Insets(10, 10, 10, 10));
        jtTxt.setMinimumSize(new java.awt.Dimension(100, 100));
        jScrollPane1.setViewportView(jtTxt);

        jTabbedPane1.addTab("Codigo Fuente", jScrollPane1);

        jtLexico.setColumns(20);
        jtLexico.setFont(new java.awt.Font("Courier New", 0, 14)); // NOI18N
        jtLexico.setRows(5);
        jtLexico.setMargin(new java.awt.Insets(10, 10, 10, 10));
        jtLexico.setMinimumSize(new java.awt.Dimension(100, 100));
        jScrollPane2.setViewportView(jtLexico);

        jTabbedPane1.addTab("Análisis Léxico", jScrollPane2);

        jtSintactico.setColumns(20);
        jtSintactico.setFont(new java.awt.Font("Courier New", 0, 14)); // NOI18N
        jtSintactico.setRows(5);
        jtSintactico.setMargin(new java.awt.Insets(10, 10, 10, 10));
        jtSintactico.setMinimumSize(new java.awt.Dimension(100, 100));
        jScrollPane3.setViewportView(jtSintactico);

        jTabbedPane1.addTab("Análisis Sintáctico", jScrollPane3);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 665, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jbSalir, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE)
                            .addComponent(jbLimpiar, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jlIndicador)
                                .addComponent(jlIndicador2, javax.swing.GroupLayout.Alignment.TRAILING))
                            .addComponent(jbVerificar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE)))
                    .addComponent(jLabel2))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(jbVerificar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbLimpiar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbSalir)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 450, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jlIndicador2)
                            .addComponent(jlIndicador)))
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 553, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents

private void jbVerificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbVerificarActionPerformed
    String texto = jtTxt.getText();
    Lexico.setTerminalLexico("");
    Sintactico.setTerminalSintactico("");
    //En este vector se encuentra las lienas del codigo fuente codificadas
    //con los codigos del archivo de codigos.csv
    //a esta variable se deberia hacer el proceso de validación de las palablas
    String[] codigo = Lexico.quitarEspaciosLineas(texto);

    //Inicio de Analisis
    int catErrores = Lexico.analizar(codigo);

    if (catErrores == 0) {
        setIconoIndicador(true);
    } else {
        setIconoIndicador(false);
    }
    jtLexico.setText(Lexico.getTerminalLexico());
    jtSintactico.setText(Sintactico.getTerminalSintactico());

    Lexico.setResetSalida();

}//GEN-LAST:event_jbVerificarActionPerformed

private void jbSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbSalirActionPerformed
    System.exit(0);
}//GEN-LAST:event_jbSalirActionPerformed

private void jbLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbLimpiarActionPerformed
    jtTxt.setText("VARIABLES \n\nINICIO \n\nFIN \n");
    jtLexico.setText("");
    Lexico.setTerminalLexico("");
    Sintactico.setTerminalSintactico("");
    jtSintactico.setText("");
    LimpiarIconos();
}//GEN-LAST:event_jbLimpiarActionPerformed

    /**
     * Pone el icono de codigo valido si es correcto, caso contrario pone un 
     * icono que representa la invalides del codigo...
     * @param op    opcion de la validacion del codigo
     */
    private void setIconoIndicador(boolean op) {
        if (op) {
            jlIndicador.setIcon(new ImageIcon("iconos/up.png"));
            jlIndicador2.setIcon(new ImageIcon("iconos/bien.png"));
        } else {
            jlIndicador.setIcon(new ImageIcon("iconos/down.png"));
            jlIndicador2.setIcon(new ImageIcon("iconos/mal.png"));
        }
    }

    /**
     * Quita los Iconos que esten en la pantalla en la opcion indicador
     */
    private void LimpiarIconos() {
        jlIndicador.setText("");
        jlIndicador.setIcon(null);
        jlIndicador2.setText("");
        jlIndicador2.setIcon(null);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Java_Espanol().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JButton jbLimpiar;
    private javax.swing.JButton jbSalir;
    private javax.swing.JButton jbVerificar;
    private javax.swing.JLabel jlIndicador;
    private javax.swing.JLabel jlIndicador2;
    private javax.swing.JTextArea jtLexico;
    private javax.swing.JTextArea jtSintactico;
    private javax.swing.JTextArea jtTxt;
    // End of variables declaration//GEN-END:variables
}
